<template>
  <div class="app-container">
    <el-card class="card-form">
      <div class="organization-title">通知公告</div>
      <el-form :model="queryForm" ref="queryForm" :inline="true">
        <el-form-item label="标题" prop="noticeTitle">
          <el-input
            v-model="queryForm.noticeTitle"
            placeholder="请输入"
            clearable
            size="small"
            style="width: 252px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="noticeStatus">
          <el-select
            v-model="queryForm.noticeStatus"
            placeholder="请选择"
            clearable
            size="small"
            style="width: 252px"
          >
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 252px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="card-table">
      <el-row :gutter="10" class="mb20 mt5">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
            v-hasPermi="['noticeManage:list:add']"
            >通知公告</el-button
          >
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="noticeList" border>
        <el-table-column label="序号" align="center" width="65" type="index" />
        <el-table-column
          label="标题"
          align="center"
          prop="noticeTitle"
          :show-overflow-tooltip="true"
          width="500"
        />
        <el-table-column label="添加时间" align="center" prop="createTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作人" align="center" prop="noticeOperator" />
        <el-table-column label="状态" align="center" prop="noticeStatus">
          <template slot-scope="scope">
            <span
              class="icon-state icon-red"
              v-show="
                scope.row.noticeStatus === '0' ||
                scope.row.noticeStatus === '未发布'
              "
              ><span></span
            ></span>
            <span
              class="icon-state icon-green"
              v-show="
                scope.row.noticeStatus === '1' ||
                scope.row.noticeStatus === '已发布' ||
                scope.row.noticeStatus === ''
              "
              ><span></span
            ></span>
            <span style="margin-left: 10px">{{
              scope.row.noticeStatus === "0" ? "未发布" : "已发布"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleView(scope.row)"
              v-show="scope.row.noticeStatus !== '0'"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.row)"
              v-show="scope.row.noticeStatus == '0'"
              v-hasPermi="['noticeManage:list:edit']"
              >编辑</el-button
            >

            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['noticeManage:list:del']"
              v-show="scope.row.noticeStatus == '0'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-show="total > 0"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="queryForm.pageSize"
        :current-page="queryForm.pageNum"
        :page-sizes="[10, 20, 30, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加或修改通知公告对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="30%"
      append-to-body
      custom-class="dialog-box"
    >
      <el-form
        ref="formData"
        :model="formData"
        :rules="formRules"
        label-width="80px"
      >
        <el-form-item label="标题" prop="noticeTitle">
          <el-input
            v-model="formData.noticeTitle"
            placeholder="请输入"
            style="width: 100%"
            maxlength="20"
          />
        </el-form-item>
        <el-form-item label="内容" prop="noticeContent">
          <el-input
            v-model="formData.noticeContent"
            type="textarea"
            placeholder="请输入"
            style="width: 100%"
            maxlength="200"
            show-word-limit
            :rows="4"
          />
        </el-form-item>
        <el-form-item label="附件" prop="noticeFile.fileName">
          <el-input
            v-model="formData.noticeFile.fileName"
            placeholder="请选择文件"
            style="width: 100%; display: none"
          />
          <div class="files_upload_box" @click="handleImport">
            {{ formData.noticeFile.fileName }}
            <div class="img_box">
              <img
                src="@/assets/images/tripartite/icon_files_upload.png"
                alt=""
              />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="发送对象" prop="noticeSend">
          <el-select
            v-model="formData.noticeSend"
            placeholder="请选择"
            multiple
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in noticeSendOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" plain @click="submitForm">保存</el-button>
        <el-button type="primary" @click="issueForm">发布</el-button>
      </div>
    </el-dialog>

    <!-- 查看通知公告对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="openDataScope"
      width="800px"
      append-to-body
      custom-class="dialog-box"
    >
      <el-descriptions class="descriptions-content-box" :column="1" border>
        <el-descriptions-item>
          <template slot="label"> 标题 </template>
          {{ DataScopeForm.noticeTitle }}
        </el-descriptions-item>
        <el-descriptions-item 
          label-class-name="descriptions-item-label "
          content-class-name="descriptions-item-content "
        >
          <template slot="label"> 内容 </template>
          {{ DataScopeForm.noticeContent }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 附件 </template>
          {{ DataScopeForm.noticeFile!=null?DataScopeForm.noticeFile.fileName:"" }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 发送对象 </template>
          {{ DataScopeForm.noticeSend }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 创建时间 </template>
          {{ DataScopeForm.createTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label"> 操作人 </template>
          {{ DataScopeForm.noticeOperator }}
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDataScope">取 消</el-button>
        <el-button type="primary" @click="cancelDataScope">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 文件上传对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="498px"
      append-to-body
      custom-class="dialog-box"
    >
      <el-upload
        ref="upload"
        :limit="1"
        :on-exceed="handleExceed"
        accept=".png, .jpg, .pdf, .xls, .doc, .docx, .zip, .rar, .7z"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :on-error="handleFileError"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入".png, .jpg, .pdf, .xls, .doc, .docx, .zip, .rar, .7z"格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
  addIssue,
  updateIssue,
} from "@/api/systemManage/notice";

export default {
  name: "Organization",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（查看）
      openDataScope: false,
      // 当前行数据
      DataScopeForm: {
        noticeFile: {
          fileName: "",
          fileUrl: "",
        },
      },
      // 日期范围
      dateRange: [],
      // 状态数据字典
      typeOptions: [
        { dictValue: "0", dictLabel: "未发表" },
        { dictValue: "1", dictLabel: "已发布" },
      ],
      // 发送对象数据字典
      noticeSendOptions: [
        { dictValue: "102", dictLabel: "项目单位" },
        { dictValue: "103", dictLabel: "评审专家" },
        { dictValue: "104", dictLabel: "三方机构" },
      ],
      // 查询参数
      queryForm: {
        pageNum: 1,
        pageSize: 10,
        startCreateTime: undefined,
        endCreateTime: undefined,
        noticeStatus: undefined,
        noticeTitle: undefined,
      },
      // 表单参数
      formData: {
        noticeFile: {
          fileName: "",
          fileUrl: "",
        },
      },
      // 表单校验
      formRules: {
        noticeTitle: [
          { required: true, message: "标题名称不能为空", trigger: "blur" },
        ],
        noticeContent: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
        noticeSend: [
          { required: true, message: "请选择发送对象", trigger: "change" },
        ],
        "noticeFile.fileName": [
          { required: true, message: "请选择文件", trigger: "change" },
        ],
      },
      // 上传文件参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/file/upload",
        // 上传文件名称
        fileName: "请选择文件",
      },
    };
  },
  created() {
    this.getList();
    // this.getDicts("sys_normal_disable").then((response) => {
    //   this.statusOptions = response.data;
    // });
  },
  methods: {
    /** 日期处理 */
    changeDate(val){
      if(val){
        this.queryForm.startCreateTime=val[0]+ ' 00:00:00'
        this.queryForm.endCreateTime=val[1]+ ' 23:59:59'
        }     
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryForm).then((response) => {
        // console.log("通告列表", response.rows);
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.formData = {
        noticeId: "",
        noticeTitle: "",
        noticeSend: [],
        noticeStatus: "0",
        noticeContent: "",
        createTime: "",
        noticeStatus: "",
        noticeTitle: "",
        noticeFile: {
          fileName: "",
          fileUrl: "",
        },
      };
      this.resetForm("formData");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryForm.startCreateTime=undefined;
      this.queryForm.endCreateTime=undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通知公告";
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.reset();
      const noticeId = row.noticeId;
      getNotice(noticeId).then((response) => {
        this.formData = response.data;
        this.formData.noticeSend = this.formData.noticeSend.split(",");
        // this.formData.noticeSend = JSON.parse(response.data.noticeSend)
        this.open = true;
        this.title = "编辑通知公告";
      });
    },
    /** 查看按钮操作*/
    handleView(row) {
      const noticeId = row.noticeId;
      getNotice(noticeId).then((response) => {
        this.DataScopeForm = response.data;
        let str = response.data.noticeSend
        if(str.indexOf("102") != -1){
          str=str.replace(102,"项目单位");
        }
        if(str.indexOf("103") != -1){
          str=str.replace(103,"评审专家");
        } 
        if(str.indexOf("104") != -1){
          str=str.replace(104,"三方机构");
        }
        this.DataScopeForm.noticeSend = str
        this.openDataScope = true;
        this.title = "查看通知公告";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（查看）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },

    /** 提交按钮 */
    submitForm: function () {
      console.log("000", this.formData);
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          if (
            this.formData.noticeId == undefined ||
            this.formData.noticeId == ""
          ) {
            this.formData.noticeSend = this.formData.noticeSend.join();
            addNotice(this.formData).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.formData.noticeSend = this.formData.noticeSend.join();
            updateNotice(this.formData).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 发布按钮操作 */
    issueForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          if (
            this.formData.noticeId == undefined ||
            this.formData.noticeId == ""
          ) {
            this.formData.noticeSend = this.formData.noticeSend.join();
            addIssue(this.formData).then((response) => {
              this.msgSuccess("发布成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.formData.noticeSend = this.formData.noticeSend.join();
            this.formData.noticeStatus = "1";
            updateIssue(this.formData).then((response) => {
              this.msgSuccess("发布成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeId = row.noticeId;
      this.$confirm("是否确认删除 ？", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "del_box",
      })
        .then(function () {
          return delNotice(noticeId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },

    /** 文件上传操作 */
    handleImport() {
      this.upload.title = "附件上传";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
      // console.log("文件上传中", file);
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log("文件上传成功", response, file);
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if(response.code!=500&&response.data){
        this.upload.fileName = file.name;
        this.formData.noticeFile.fileUrl = response.data.fileUrl;
        this.formData.noticeFile.fileName = file.name;
      }else{
        this.$message.error(response.msg)
      }
      
    },
    // 文件上传失败处理
    handleFileError(err, file, fileList){
      console.log(999,err, file, fileList);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 分页 */
    handleSizeChange(val) {
      this.queryForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val;
      this.getList();
    },
  },
};
</script>
<style scoped lang="scss">
.organization-title {
  font-size: 18px;
  font-family: PingFang SC, PingFang SC-Medium;
  font-weight: 500;
  margin-bottom: 24px;
  color: #000000;
}
.card-table {
  margin-top: 10px;
  .el-pagination {
    text-align: center;
    margin-top: 40px;
  }
  // 状态样式
  .icon-state {
    display: inline-block;
    width: 16px;
    height: 16px;
    border-radius: 16px;
    position: relative;
    top: 3.5px;
    span {
      display: inline-block;
      margin: 5px;
      width: 7px;
      height: 7px;
      border-radius: 16px;
    }
  }
  .icon-red {
    background: rgba(223, 32, 66, 0.1);
    span {
      background: #df2042;
    }
  }
  .icon-green {
    background: rgba(51, 168, 61, 0.1);
    span {
      background: #33a83d;
    }
  }
}
// 文件上传
.files_upload_box {
  width: 100%;
  height: 36px;
  line-height: 36px;
  font-size: 13px;
  font-family: PingFang SC, PingFang SC-Regular;
  color: #afb3bd;
  border: 1px solid #d8d8d8;
  border-radius: 4px;
  padding-left: 15px;
  position: relative;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-right: 30px;
  .img_box {
    height: 36px;
    width: 36px;
    background: #0d72ff;
    text-align: center;
    border-radius: 0px 4px 4px 0px;
    position: absolute;
    right: -1px;
    top: -1px;
    img {
      width: 16px;
      height: 16px;
    }
  }
}
</style>
<style lang="scss">
/**表单label取消加粗 */
.card-form {
  .el-card__body {
    padding-bottom: 0;
  }
  label {
    font-weight: normal !important;
  }
  .el-form--inline .el-form-item {
    margin-right: 60px;
  }
}
/* 弹出窗样式 */
.dialog-box {
  .el-dialog__body {
    padding: 24px 69px 24px 69px;
    border-top: 1px solid #ebecef;
    border-bottom: 1px solid #ebecef;
    label {
      font-weight: 400 !important;
    }
  }
  .el-dialog__footer {
    padding: 16px 20px;
  }
  .el-dialog__headerbtn {
    background: url("../../../assets/images/icon_dialog_close.png");
    background-size: 100%;
    width: 16px;
    height: 16px;
    .el-dialog__close {
      visibility: hidden;
    }
  }
  .descriptions-content-box{
    width: 100%;
    .descriptions-item-label{
      width: 120px;
    }
  }
}
/* el-descriptions-item 文字居中 */
.el-descriptions__body .el-descriptions__table .el-descriptions-item__cell {
  text-align: center;
}
/* 删除弹出框样式 */
.del_box {
  width: 345px;
  height: 175px;
  .el-message-box__headerbtn {
    background: url("../../../assets/images/icon_dialog_close.png");
    background-size: 100%;
    width: 16px;
    height: 16px;
    .el-message-box__close {
      visibility: hidden;
    }
  }
  .el-message-box__content {
    padding: 26px 95px;
    font-size: 16px;
    .el-message-box__status.el-icon-warning {
      color: #ea4747;
    }
    .el-message-box__status + .el-message-box__message {
      padding-right: 0;
    }
  }
}
</style>
